package jp.co.ricoh.melody.views.fragments;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.databinding.AlarmListFragmentBinding;
import jp.co.ricoh.melody.presenters.AlarmListFragmentPresenter;
import jp.co.ricoh.melody.presenters.IAlarmListFragmentPresenter;
import jp.co.ricoh.melody.views.activities.MainActivity;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 */
public class AlarmListFragment extends Fragment
        implements IAlarmListFragmentView {
    private static final int CREATE_VIEW_DELAY_TIME = 100;

    public interface OnAlarmListScrollListener {
        void onStateChanged(boolean scrolling);
    }

    private AlarmListFragmentBinding mBinding;
    private IAlarmListFragmentPresenter mPresenter;
    private OnAlarmListScrollListener mOnAlarmListScrollListener;
    private Handler mHandler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_alarm_list, container, false);
        mPresenter = new AlarmListFragmentPresenter();
        mPresenter.onCreateView(this);
        mBinding.alarmRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mBinding.alarmRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                mOnAlarmListScrollListener.onStateChanged(newState == 1 ? true : false);
            }
        });
        mBinding.alarmRecyclerView.setActivity((MainActivity) getActivity());
        return mBinding.getRoot();
    }

    public void setOnAlarmListScrollListener(OnAlarmListScrollListener onAlarmListScrollListener) {
        mOnAlarmListScrollListener = onAlarmListScrollListener;
    }

    public void setRecycleView(final RelativeLayout recycleBinContainer, final ImageView recycleBinImg) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (getView() != null) {
                    mBinding.alarmRecyclerView.setRecycleView(recycleBinContainer, recycleBinImg);
                } else {
                    mHandler.postDelayed(this, CREATE_VIEW_DELAY_TIME);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBinding.alarmRecyclerView.onDestroy();
        mPresenter.onDestroyView();
    }

    public static AlarmListFragment newInstance() {
        return new AlarmListFragment();
    }
}
